<?php

namespace App\Models\Ddaman;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\common\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class WorkflowDesign extends Base
{
    use HasFactory,SoftDeletes;

    protected $table = "workflow_design";

    //const CREATED_AT = null;
    //const UPDATED_AT = null;

    protected $fillable = [
        'guid','title','cid','sign','sort','description','is_delete','status'
    ];


    public $rule = [
        'title' => 'required|string|between:2,100',
        'cid' => 'required',
    ];

    public $editRule = [
        'id' => 'required',
        'title' => 'required|string|between:2,100',
        'cid' => 'required',
    ];

    public $message = [
        'id.required' => '标识必须',
        'name.required' => '名称必须填写',
        'cid.required'=>"请选择流程类型",
    ];

    /*
     * 获取列表
     * */
    public function getList( $param = [] )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $where = [];

        if (isset($param['keywords']) && $param['keywords']){
            $keywords = trim($param['keywords']);
            $where[] = ['name','like',"%$keywords%"];
        }

        $field = array_merge(['id'],$this->fillable);
        $list = $this->where($where)->orderBy('id','desc')->select($field)->paginate($limit);

        return $list;

    }

    /*
     * 获取工作列表
     * */
    public static function getWorkList()
    {
        $field = [
            'id','guid','cid','title'
        ];
        $where = [];
        $identify = get_identify();
        switch ($identify){
            case 0:
                $design_ids = [16,17,18,19];
                $where[] = [function ($query) use($design_ids){
                   $query->whereIn('id',$design_ids);
                }];
                break;
            case 3:
                $design_ids = [15];
                $where[] = [function($query) use ($design_ids){
                    $query->whereIn('id',$design_ids);
                }];
                break;
        }

        $list = WorkflowDesign::where($where)->select($field)->orderBy('sort','desc')->get();
        return $list;
    }

    /*
     * 获取工作流类型
     * */
    public function getWorkflowType()
    {
        return DB::table('workflow_type')
            ->where('status',1)
            ->get();
    }




}
